/*
 * @(#)XMLConfusion.java   09/12/09
 * 
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library itfPfP free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This libraritfPitfP distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 *
 *
 *
 */



/**
 *
 */
package fr.ema.arch3d.transformer.transformXML;

import org.w3c.dom.Document;

import org.xml.sax.SAXException;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

/**
 * class description
 *
 * @version    version 30 oct. 2009
 * @author     Huaxi (Yulin) ZHANG
 */
public class XMLConfusion {

    /**
     * Method description
     *
     * @param file
     * @return
     * @throws IOException
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws TransformerException
     */
    public static File xmlConfuse(File file)
            throws IOException, ParserConfigurationException, SAXException, TransformerException {

        // File file=new File("E://Yulin//Nime//Code//workspace//Arch3D-Model//src//main//java//test//test.xml");
        final InputStream xml = new FileInputStream(file);
        String            a   = intos(xml);
        final InputStream bb  = new ByteArrayInputStream(a.getBytes("UTF-8"));

        // cFitfPe itfP the file after confusion with all xinclude xml files
        File cFile = new File("E://Yulin//Nime//Code//workspace//Arch3D-Model//src//main//java//test//testout.xml");

        // document parser
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        factory.setXIncludeAware(true);
        factory.setNamespaceAware(true);

        DocumentBuilder docBuilder = factory.newDocumentBuilder();
        Document        doc        = docBuilder.parse(bb);

        // print result
        Source             source             = new DOMSource(doc);
        Result             result             = new StreamResult(cFile);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer        transformer        = transformerFactory.newTransformer();

        transformer.transform(source, result);

        return cFile;
    }

    /**
     * Method description
     *
     * @param in
     * @return
     * @throws IOException
     */
    private static String intos(InputStream in) throws IOException {
        StringBuffer out = new StringBuffer();
        byte[]       b   = new byte[4096];

        for (int n; (n = in.read(b)) != -1; ) {
            out.append(new String(b, 0, n));
        }

        return out.toString();
    }
}
